package com.it135.citytraffic.service.impl;

import com.it135.citytraffic.mapper.ReportMapper;
import com.it135.citytraffic.mapper.TrafficInfoMapper;
import com.it135.citytraffic.pojo.dto.RoadJamCountReportDTO;
import com.it135.citytraffic.pojo.vo.EchartsSeriesVO;
import com.it135.citytraffic.pojo.vo.TrafficInfoAverageVO;
import com.it135.citytraffic.service.ReportService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Data
@Slf4j
public class ReportServiceImpl implements ReportService {

    private final ReportMapper reportMapper;
    private final TrafficInfoMapper trafficInfoMapper;

    //    获取路段的统计图
    @Override
    public List<RoadJamCountReportDTO> getRoadJamCount(Integer crossing_id, Integer status) {
        return reportMapper.getRoadJam(crossing_id, status);
    }

    //    获取路口的统计图
    @Override
    public List<EchartsSeriesVO> getCrossingReport(Integer crossingId, LocalDate beginDate, LocalDate endDate) {
//       创建了两个返回对象
        EchartsSeriesVO green = new EchartsSeriesVO("畅通比", "line", "green", null);
        EchartsSeriesVO yellow = new EchartsSeriesVO("缓行比", "line", "#baa381", null);
        EchartsSeriesVO red = new EchartsSeriesVO("拥堵比", "line", "red", null);
        ArrayList<Float> greenData = new ArrayList<>();
        ArrayList<Float> yellowData = new ArrayList<>();
        ArrayList<Float> redData = new ArrayList<>();
        LocalDateTime variableDate = beginDate.atStartOfDay();
        LocalDateTime endTime = endDate.atStartOfDay();
        while (true) {
            LocalDateTime startOfDay = variableDate.with(LocalTime.MIN);
            LocalDateTime endOfDay = variableDate.with(LocalTime.MAX);

            TrafficInfoAverageVO trafficInfoAverageVO = trafficInfoMapper.getAvgByCrossingId(crossingId, startOfDay, endOfDay);
            if (trafficInfoAverageVO == null) {
                greenData.add(0f);
                yellowData.add(0f);
                redData.add(0f);
            }else{
                greenData.add(Float.valueOf(trafficInfoAverageVO.getExpediteAvg()));
                yellowData.add(Float.valueOf(trafficInfoAverageVO.getCongestedAvg()));
                redData.add(Float.valueOf(trafficInfoAverageVO.getBlockedAvg()));
            }

            variableDate=variableDate.plusDays(1);
            if (variableDate.isAfter(endTime)) {
                break;
            }
        }
        green.setData(greenData);
        yellow.setData(yellowData);
        red.setData(redData);

        ArrayList<EchartsSeriesVO> echartsSeriesVOS = new ArrayList<>();
        echartsSeriesVOS.add(green);
        echartsSeriesVOS.add(yellow);
        echartsSeriesVOS.add(red);
        return echartsSeriesVOS;
    }


}
